package Persistence;

import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.transaction.UserTransaction;

/**
 *
 * @author Filippi, Gattavecchia, Novara 
 */
@Entity

public class UtenteRegistrato implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    public Long getId() {
        return id;
    }
    protected String nome;
    protected String cognome;
    protected String telefono;
    protected boolean bannato;
    
    @OneToOne
    protected IndirizzoEmail indirizzoEmail;
    @OneToOne
    protected IndirizzoAbitazione indirizzo;
    @OneToMany
    protected List<Annuncio> annunciCreati;
    @OneToMany(mappedBy = "iscritto")
    protected List<Adesione> adesioni;
    @OneToMany
    protected List<Feedback> feedbacks;

    /**
     * Get the value of feedbacks
     *
     * @return the value of feedbacks
     */
    public List<Feedback> getFeedbacks() {
        return feedbacks;
    }

    /**
     * Set the value of feedbacks
     *
     * @param feedbacks new value of feedbacks
     */
    public void setFeedbacks(List<Feedback> feedbacks) {
        this.feedbacks = feedbacks;
    }

    /**
     * Get the value of adesioni
     *
     * @return the value of adesioni
     */
    public List<Adesione> getAdesioni() {
        return adesioni;
    }

    /**
     * Set the value of adesioni
     *
     * @param adesioni new value of adesioni
     */
    public void setAdesioni(List<Adesione> adesioni) {
        this.adesioni = adesioni;
    }

    /**
     * Get the value of annunciCreati
     *
     * @return the value of annunciCreati
     */
    public List<Annuncio> getAnnunciCreati() {
        return annunciCreati;
    }

    /**
     * Set the value of annunciCreati
     *
     * @param annunciCreati new value of annunciCreati
     */
    public void setAnnunciCreati(List<Annuncio> annunciCreati) {
        this.annunciCreati = annunciCreati;
    }

    /**
     * Get the value of indirizzo
     *
     * @return the value of indirizzo
     */
    public IndirizzoAbitazione getIndirizzo() {
        return indirizzo;
    }

    /**
     * Set the value of indirizzo
     *
     * @param indirizzo new value of indirizzo
     */
    public void setIndirizzo(IndirizzoAbitazione indirizzo) {
        this.indirizzo = indirizzo;
    }

    /**
     * Get the value of indirizzoEmail
     *
     * @return the value of indirizzoEmail
     */
    public IndirizzoEmail getIndirizzoEmail() {
        return indirizzoEmail;
    }

    /**
     * Set the value of indirizzoEmail
     *
     * @param indirizzoEmail new value of indirizzoEmail
     */
    public void setIndirizzoEmail(IndirizzoEmail indirizzoEmail) {
        this.indirizzoEmail = indirizzoEmail;
    }

    /**
     * Get the value of bannato
     *
     * @return the value of bannato
     */
    public boolean isBannato() {
        return bannato;
    }

    /**
     * Set the value of bannato
     *
     * @param bannato new value of bannato
     */
    public void setBannato(boolean bannato) {
        this.bannato = bannato;
    }

    /**
     * Get the value of telefono
     *
     * @return the value of telefono
     */
    public String getTelefono() {
        return telefono;
    }

    /**
     * Set the value of telefono
     *
     * @param telefono new value of telefono
     */
    public void setTelefono(String telefono) {
        this.telefono = telefono;
    }

    /**
     * Get the value of cognome
     *
     * @return the value of cognome
     */
    public String getCognome() {
        return cognome;
    }

    /**
     * Set the value of cognome
     *
     * @param cognome new value of cognome
     */
    public void setCognome(String Cognome) {
        this.cognome = Cognome;
    }

    /**
     * Get the value of nome
     *
     * @return the value of nome
     */
    public String getNome() {
        return nome;
    }

    /**
     * Set the value of nome
     *
     * @param nome new value of nome
     */
    public void setNome(String Nome) {
        this.nome = Nome;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof UtenteRegistrato)) {
            return false;
        }
        UtenteRegistrato other = (UtenteRegistrato) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Persistence.UtenteRegistrato[ id=" + id + " ]";
    }

    public void persist(Object object) {
        /* Add this to the deployment descriptor of this module (e.g. web.xml, ejb-jar.xml):
         * <persistence-context-ref>
         * <persistence-context-ref-name>persistence/LogicalName</persistence-context-ref-name>
         * <persistence-unit-name>ShippingShare-ejbPU</persistence-unit-name>
         * </persistence-context-ref>
         * <resource-ref>
         * <res-ref-name>UserTransaction</res-ref-name>
         * <res-type>javax.transaction.UserTransaction</res-type>
         * <res-auth>Container</res-auth>
         * </resource-ref> */
        try {
            Context ctx = new InitialContext();
            UserTransaction utx = (UserTransaction) ctx.lookup("java:comp/env/UserTransaction");
            utx.begin();
            EntityManager em = (EntityManager) ctx.lookup("java:comp/env/persistence/LogicalName");
            em.persist(object);
            utx.commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            throw new RuntimeException(e);
        }
    }
    
}
